'use client';

import { queryMessage } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { IMessage } from '@/interfaces';
import Alert from '@/app/[locale]/alert/alert';
import AlertLoad from '@/app/[locale]/alert/load';
import { useContext } from 'react';
import QueryMessageInfo from '@/app/[locale]/admin/message/detail/info';
import { MessageAdminContext } from '@/contexts/admin/message';

export default function DetailsMessageAdminPage() {
  const context = useContext(MessageAdminContext);

  const queryMessageQuery = useQuery(
    ['/messages', context.detailItem?.id],
    async (context) => {
      return (await queryMessage({
        id: context.queryKey[1] + '',
      })) as IMessage;
    },
  );

  if (queryMessageQuery.data) {
    const data = queryMessageQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryMessageInfo details={data} />

            <div></div>
          </div>
        </div>
      </div>
    );
  }

  if (queryMessageQuery.error) {
    return <Alert type="error" message={queryMessageQuery.error} />;
  }

  return <AlertLoad />;
}
